package jalus.service;

import jalus.dao.TagDAO;
import jalus.model.Tag;
import jalus.utils.PojoHandler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;

public class TagService {
	private TagDAO tagDAO;
	private Cache cache;
	private static TagService instance = new TagService();
	private TagService(){
		tagDAO = new TagDAO();
		try {
			CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
			cache = cacheFactory.createCache(Collections.emptyMap());
		} catch (CacheException e) {
		}
	}
	public static TagService getInstance(){
		return instance;
	}
	
	private static String TAG = "tag";
	//TODO CountService
	private static String COUNTER = "counter";
	
	/**
	 * 获取帖子总数目
	 * @param tag tag=null 时，获得所有文章数目
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public int getPostNum(String tagString){
		String key = "getPostNum@" + tagString;
		if(cache.containsKey(key)){
			return (Integer) cache.get(key);
		}
		Tag tag;
		if(tagString == null){
			tag = tagDAO.getTagByName(COUNTER, "postnum");
		}else{
			tag = tagDAO.getTagByName(TAG, tagString);
		}
		int count;
		if(tag == null){
			count = 0;
		}else{
			count = tag.getCount();
		}
		cache.put(key, count);
		return count;
	}
	
	/**
	 * 获得所有的tag"
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<jalus.pojo.Tag> getAllTag(){
		String key= "getAllTag";
		if(!cache.containsKey(key)){
			List<jalus.pojo.Tag> tagList = PojoHandler.copyMToPList(tagDAO.getListByCid(TAG));
			cache.put(key, tagList);
		}
		return (List<jalus.pojo.Tag>) cache.get(key);
	}

	/**
	 * 更改tag计数
	 * @param oldTags
	 * @param newTags
	 */
	public void updateTags(List<String> oldTags, List<String> newTags){
		if(oldTags == null){
			oldTags = new ArrayList<String>(0);
		}
		if(newTags == null){
			newTags = new ArrayList<String>(0);
		}
		Set<String> oldTagSet = new HashSet<String>(oldTags);
		Set<String> newTagSet = new HashSet<String>(newTags);
		for(String tag : oldTagSet){
			if(!newTagSet.contains(tag)){
				decreaseCountByName(tag);
			}
		}
		for(String tag : newTagSet){
			if(!oldTagSet.contains(tag)){
				increaseCountByName(tag);
			}
		}
		cache.clear();
	}
	/**
	 * 按照post中的结果，重新计算tag的count
	 */
	public void recaculateTagCount(){
		//TODO: 重新计算tag的count

	}
	
	/**
	 * tag计数增1
	 * @param tagName
	 */
	private void increaseCountByName(String tagName){
		tagDAO.updateCountByName(TAG, tagName, 1);
	}
	
	/**
	 * tag计数减1
	 * @param tagName
	 */
	private void decreaseCountByName(String tagName){
		tagDAO.updateCountByName(TAG, tagName, -1);
	}
	
	/**
	 * 文章总数+1
	 */
	public void decreasePostCount() {
		tagDAO.updateCountByName(COUNTER, "postnum", -1);
		cache.clear();
	}
	
	/**
	 * 文章总数-1
	 */
	public void increasePostCount() {
		tagDAO.updateCountByName(COUNTER, "postnum", 1);
		cache.clear();
	}
	
	public void transfer(){
/*		List<Tag> tagList = tagDAO.getAllTag();
		for(Tag tag : tagList){
			if(tag.getName().equals("all")){
				tagDAO.addTag(new Tag(COUNTER, "postnum", tag.getCount()));
				tagDAO.delete(tag.getId());
			}else{
				tagDAO.addTag(new Tag(TAG, tag.getName(), tag.getCount()));
				tagDAO.delete(tag.getId());
			}
		}
		String key= "alltaglist";
		CommonCache.getInstance().getCache().remove(key);*/
		cache.clear();
	}
}
